
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Array;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Bidya
 */
public class ClientDetails {
    String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    final DateFormat timeFormat = new SimpleDateFormat("HH : mm : ss");
    JFrame frame = new JFrame("client Information");
    JDesktopPane pane = new JDesktopPane();
    JPanel p0 = new JPanel();
    
    JLabel lbl0 = new JLabel();
    JLabel lbl1 = new JLabel();
    JLabel lbl2 = new JLabel();
    JLabel lbdate = new JLabel();
    JLabel lbtime = new JLabel();
    JLabel name1=new JLabel();
    JLabel empid=new JLabel();
    JTextField nametxt=new JTextField();
    JTextField idtext=new JTextField();
    JButton clint=new JButton();
    JScrollPane scp = new JScrollPane();
    JButton chain=new JButton("Register");
    JButton exit=new JButton("Exit");
    

    AutoTextComplete nameact = new AutoTextComplete(nametxt);
   JPanel det= new JPanel();
    
    //-------Display Variable------------//
    JLabel lbname=new JLabel();
    JLabel vlname=new JLabel();
    JLabel lbadd=new JLabel();
    JLabel vladd=new JLabel();
    JLabel lbphn=new JLabel();
    JLabel vlphn=new JLabel();
    JLabel lbgft=new JLabel();
    JLabel vlgift=new JLabel();
    JLabel lbvehc=new JLabel();
    JLabel vlvehc=new JLabel();
    JLabel lbagnt=new JLabel();
    JLabel vlagnt=new JLabel();
    JLabel lbcln1=new JLabel();
    JLabel  vlcln1=new JLabel();
    JLabel lbcln2=new JLabel();
    JLabel  vlcln2=new JLabel();
    
    

    public ClientDetails() throws Exception {
        frame.setLocation(150, 30);
        frame.setVisible(true);
        frame.setSize(900, 600);
        frame.setResizable(false);
        frame.add(pane);
        pane.setBounds(0, 0, 900, 600);
        pane.setBackground(Color.pink);
        pane.add(lbl0);
        lbl0.setBounds(230, 10, 500, 40);
        lbl0.setText("Client Information");
        lbl0.setForeground(Color.GREEN);
        lbl0.setFont(new Font("Colonna MT", Font.ITALIC, 35));
        lbl0.setHorizontalAlignment(JLabel.CENTER);
        pane.add(lbdate);
        lbdate.setBounds(20, 32, 55, 20);
        lbdate.setText("Date:-");
        lbdate.setForeground(Color.blue);
        lbdate.setFont(new Font("Bell MT", Font.ITALIC, 18));
        
        pane.add(lbl1);
        lbl1.setBounds(70, 30, 100, 30);
        lbl1.setFont(new Font("Bell MT", Font.ITALIC, 18));
        lbl1.setHorizontalAlignment(JLabel.LEFT);
        lbl1.setForeground(Color.BLUE);
        lbl1.setText("" + date);

        pane.add(lbl2);
        lbl2.setBounds(20, 50, 150, 30);
        lbl2.setFont(new Font("Bell MT", Font.ITALIC, 18));
        lbl2.setHorizontalAlignment(JLabel.LEFT);
        final Date date = new Date();
        String time = timeFormat.format(date);
        lbl2.setText("Time - " + time);
        lbl2.setForeground(Color.BLUE);
        
        pane.add(p0);
        p0.setBounds(20, 120, 850, 60);
        p0.setBackground(Color.LIGHT_GRAY);
        p0.setLayout(null);
        p0.setBorder(BorderFactory.createTitledBorder(null, " Client Search Information",
				2, 2, null, Color.red));
       //----lAST TWO BUTTON------
        pane.add(chain);
        chain.setBounds( 570, 500, 100, 40);
        chain.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
            
           new ChainRegistration();
            frame.dispose();
            }
            
        });
        pane.add(exit);
        exit.setBounds( 680, 500, 100, 40);
       //---Name-------
        exit.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
         	   frame.dispose();
            }
        });
        
        p0.add(name1);
        name1.setBounds(80, 16, 60, 30);
        name1.setText("Name:");
        name1.setFont(new Font("Berlin Sans FB",Font.HANGING_BASELINE,15));
        name1.setForeground(Color.BLUE);
        
        p0.add(nametxt);
        nametxt.setBounds(150, 16, 130, 30);
        
        //------------EMPLOYEE ID----------
        p0.add(empid);
        empid.setBounds(360, 16, 100, 30);
        empid.setText("EMPID:");
        empid.setFont(new Font("Berlin Sans FB",Font.HANGING_BASELINE,15));
        empid.setForeground(Color.BLUE);
        
        p0.add(idtext);
        idtext.setBounds(470, 16, 130, 30);
        p0.add(clint);
        clint.setBounds(640, 16, 90, 30);
        clint.setText("Click");
        
        //----------------Auto Clint Info-------------//
        
         try{
            DBManager db = null;
             Connection connection = db.openDBConnection();
              new AutoCompleteDBLink(nameact, "select name from chain.registration order by name;", connection);
            
           
           
       }
        catch (Exception e5) {
        }
    nametxt.addKeyListener(new KeyListener() {
        DBManager db = null;
               Connection connection = db.openDBConnection();
                 ResultSet rs=null;
                  String empid=null;

            public void keyTyped(KeyEvent e) {}


            public void keyPressed(KeyEvent e) {}


            public void keyReleased(KeyEvent e) {
              String member=nametxt.getText();
               try{
              
               String sql1="SELECT  EMPID from CHAIN.REGISTRATION WHERE NAME =  '"+member+"';";
               PreparedStatement ps=connection.prepareStatement(sql1);
               rs=ps.executeQuery();
               if(rs.next()){
                  empid=rs.getString(1);
                  idtext.setText(empid);
                    
               }
                   
             // connection.close();
               }catch(Exception e1){}
           }
        });     
    //--------------------------KEY FUNCTION FINSHED-------------------//
    
     pane.add(det);
    det.setBounds(50, 200, 500, 300);
    det.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
    det.setFont(new Font("Arial",Font.BOLD,20));
    det.setLayout(null);
    det.setBorder(BorderFactory.createTitledBorder(null, " Client Data",
				2, 2, null, Color.red));
   clint.addMouseListener(new MouseAdapter() {
   
    public void mouseClicked(MouseEvent e) {
    
   
    try{
        DBManager dbm=null;
        Connection con=dbm.openDBConnection();
        String tb=idtext.getText();
        String sql12="Select * from CHAIN.registration where EMPID='"+ tb + "';";
        PreparedStatement ps = (PreparedStatement) con.prepareStatement(sql12);
	ResultSet rs = ps.executeQuery();
        while(rs.next()){
            String fn=rs.getString(2);
            String vehc=rs.getString(3);
            String phn=rs.getString(4);
            String gift=rs.getString(5);
            String add=rs.getString(6);
           
       
       det.add(lbname);
       lbname.setBounds(20,30,80,20);
       lbname.setText("Full Name:");
       det.add(vlname);
       vlname.setBounds(110, 30, 200, 20);
       vlname.setText(fn);
       
      det.add(lbvehc);
      lbvehc.setBounds(20,60,80,20);
      lbvehc.setText("Vehc_Reg:");
      det.add(vlvehc);
      vlvehc.setBounds(110, 60, 200, 20);
      vlvehc.setText(vehc);
      
      det.add(lbphn);
      lbphn.setBounds(20,90,80,20);
      lbphn.setText("Phn_No:");
      det.add(vlphn);
      vlphn.setBounds(110, 90, 200, 20);
      vlphn.setText(phn);
      
      det.add(lbgft);
      lbgft.setBounds(20,120,80,20);
      lbgft.setText("Gift:");
      det.add(vlgift);
      vlgift.setBounds(110, 120, 200, 20);
      vlgift.setText(gift);
    
      det.add(lbadd);
      lbadd.setBounds(20,150,80,20);
      lbadd.setText("Address:");
      det.add(vladd);
      vladd.setBounds(110, 150,200, 20);
      vladd.setText(add);
      
      
            
        }
        
    } catch(Exception e1){}  
  //----------Agent Data Upload-----------
    
    
    try{
        String member=nametxt.getText();
        DBManager dbm=null;
        Connection con=dbm.openDBConnection();
        String tb=idtext.getText();
        String sql12="Select * from CHAIN.AGENT where EMPID='"+ tb + "';";
        PreparedStatement ps = (PreparedStatement) con.prepareStatement(sql12);
	ResultSet rs = ps.executeQuery();
        while(rs.next()){
        String agent=rs.getString(3);    
       det.add(lbagnt);
      lbagnt.setBounds(20,180,80,20);
      lbagnt.setText("AgentName:");
      det.add(vlagnt);
      vlagnt.setBounds(110, 180, 200, 20);
      vlagnt.setText(agent);
            
    
        }
     }
         catch(Exception e4){} 
  //-----------------------end------------
    
     try{
        String member=nametxt.getText();
        DBManager dbm=null;
        Connection con=dbm.openDBConnection();
        String sql12="Select * from CHAIN.AGENT where agntname='"+ member + "';";
         PreparedStatement ps = (PreparedStatement) con.prepareStatement(sql12);
	ResultSet rs = ps.executeQuery();
    List<String> cln=new ArrayList<String>();
	
        while(rs.next()){
        cln.add(rs.getString(2));
        }
       StringBuffer sb=new StringBuffer();
       for (String s : cln)
       {
           sb.append(s);
           sb.append(",");
       }
        
        System.out.println(cln);
        System.out.println(sb);
        String clna=sb.toString();
        String clname[]=clna.split(",");
        
        det.add(lbcln1);
        lbcln1.setBounds(20,210,80,20);
        lbcln1.setText("ClientName1:");
        det.add(vlcln1);
        vlcln1.setBounds(110, 210, 200, 20);
        vlcln1.setText(clname[0]);
        
        det.add(lbcln2);
        lbcln2.setBounds(20,240,80,20);
        lbcln2.setText("ClientName2:");
        det.add(vlcln2);
        vlcln2.setBounds(110, 240, 200, 20);
        vlcln2.setText(clname[1]);
        
   
         
         
     }catch(Exception e8){
         
     }
     
     
     }
    
    
   });
   
    
    }
   public static void main(String args[]) throws Exception{
       new ClientDetails();
   }
    
}
